package edu.dayu.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class ThreadConfig {
    @Value("${threadPool.corePoolSize}")
    private Integer corePoolSize;

    @Value("${threadPool.maximumPoolSize}")
    private Integer maximumPoolSize;

    @Value("${threadPool.queueCapacity}")
    private Integer queueCapacity;

    @Bean(value = "threadPool")
    public ExecutorService executorService(){
        return new ThreadPoolExecutor(corePoolSize,maximumPoolSize,10L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(queueCapacity),
                // 这是谷歌的构建工具
                new ThreadFactoryBuilder().setNameFormat("demo-thread-%d").build(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
